﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Tema3.Models
{
    public class BitcointClient
    {
        Dictionary<int, BitcoinTrade> dictTrades;
        private double totalAmount;
        private double totalPrice;

        public BitcointClient(string connectionId)
        {
            dictTrades = new Dictionary<int, BitcoinTrade>();
            totalAmount = 0.0;
            totalPrice = 0.0;
            this.ConnectionId = connectionId;
        }

        public string ConnectionId
        {
            get;
            set;
        }

        public double TotalAmount { get { return totalAmount; } }
        public double TotalPrice { get { return totalPrice; } }

        public TradeResponse AddTrade(BitcoinTrade trade)
        {
            TradeResponse response;

            if (!dictTrades.ContainsKey(trade.Id))
            {
                dictTrades.Add(trade.Id, trade);
                totalAmount += trade.Amount;
                totalPrice += trade.Price * trade.Amount;

                totalAmount = Math.Round(totalAmount, 7);
                totalPrice = Math.Round(totalPrice, 7);

                response = new TradeResponse("The trade " + trade.Id + " was added succesfuly.", totalAmount, totalPrice);
            }
            else
            {
                response = new TradeResponse("The trade " + trade.Id + " is already added!", totalAmount, totalPrice);
            }

            return response;
        }

        public TradeResponse RemoveTrade(int tradeId)
        {
            TradeResponse response;

            if (dictTrades.ContainsKey(tradeId))
            {
                BitcoinTrade trade = dictTrades[tradeId];
                totalAmount -= trade.Amount;
                totalPrice -= (trade.Price * trade.Amount);

                totalAmount = Math.Round(totalAmount, 7);
                totalPrice = Math.Round(totalPrice, 7);
                
                if (totalAmount < 0.0)
                {
                    totalAmount = 0.0;
                }

                if (totalPrice < 0.0)
                {
                    totalPrice = 0.0;
                }
                dictTrades.Remove(tradeId);

                response = new TradeResponse("The trade " + tradeId + " was removed succesfuly.", totalAmount, totalPrice);
            }
            else
            {
                response = new TradeResponse("The trade " + tradeId + " could not be found on the server!",
                    totalAmount, totalPrice);
            }

            return response;
        }

        public TradeResponse Clear()
        {
            dictTrades.Clear();
            totalPrice = 0.0;
            totalAmount = 0.0;

            TradeResponse response = new TradeResponse("The saved content has been cleared!", 0, 0);
            return response;
        }
    }
}